import axios from 'axios'
import { Message } from 'element-ui'
import { removeitem } from '../utils/auth'
import router from '../router/index'
import { getitem } from '../utils/auth'
const _axios = axios.create({
  baseURL: process.env.VUE_APP_URL
})
_axios.interceptors.request.use(
  config => {
    config.headers.Authorization = `Bearer ${getitem()}`
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
_axios.interceptors.response.use(
  res => {
    return res.data
  },
  error => {
    if (error.response.data && error.response.data.message) {
      Message.error(error.response.data.message)
    }
    if (error.response.status === 401) {
      removeitem()
      router.push('/')
    }
    return Promise.reject(error)
  }
)
export default _axios
